package com.wyl.template.config.shiro;

import com.alibaba.fastjson.JSON;
import com.wyl.template.domain.model.SysPermissionDO;
import com.wyl.template.domain.model.SysRoleDO;
import com.wyl.template.domain.model.SysUserDO;
import com.wyl.template.service.SysPermissionService;
import com.wyl.template.service.SysRoleService;
import com.wyl.template.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: wyl
 * @Date: 2021/4/29
 * @Description: 自定义realm
 */
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysPermissionService permissionService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        SysUserDO userDO = (SysUserDO)principals.getPrimaryPrincipal();

        List<SysRoleDO> roleDOS = roleService.listUserRoles(userDO.getId());
        List<SysPermissionDO> permissionDOS = permissionService.listUserPermissions(userDO.getId());

        List<String> stringRoleList = new ArrayList<>();
        List<String> stringPermissionList = new ArrayList<>();

        for (SysRoleDO role : roleDOS) {
            stringRoleList.add(role.getRoleName());
        }

        for (SysPermissionDO permission : permissionDOS) {
            stringPermissionList.add(permission.getComponentName());
        }

//        System.err.println(JSON.toJSONString(stringRoleList));
//        System.err.println(JSON.toJSONString(stringPermissionList));

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(stringRoleList);
        simpleAuthorizationInfo.addStringPermissions(stringPermissionList);

        return simpleAuthorizationInfo;
    }



    /**
     * 身份认证-用户登录的时候会调用
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //从token获取用户信息，token代表用户输入
        String username = (String) token.getPrincipal();
        SysUserDO user = userService.getUserByLoginName(username);
        //取密码
        String pwd = user.getPassword();
        if (!StringUtils.hasText(pwd)) {
            return null;
        }
        //加盐
        ByteSource credentialsSalt = ByteSource.Util.bytes(user.getSalt());
        return new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, this.getClass().getName());
    }
}
